﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Defecter.Gui.Access;
using DefectDBTypes;
using System.IO;



namespace Defector
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class newUser : Window
    {

        public SqlConnection con;

        public newUser()
        {
            InitializeComponent();
        }


        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void createbtn_Click(object sender, RoutedEventArgs e)
        {
            if (passwordinput.Password != "" && firstnameinput.Text != "" && lastnameinput.Text != "" && addressinput.Text != "" && emailinput.Text != "" && phoneinput.Text != "")
            {

                String password = passwordinput.Password;
                int type = typeBox.SelectedIndex;
                String firstName = firstnameinput.Text;
                String lastName = lastnameinput.Text;
                String address = addressinput.Text;
                String email = emailinput.Text;
                String phone = phoneinput.Text;

                string sqlIns = "INSERT INTO People (First_Name, Last_Name, Address, Email, Phone_Number, Password, Type) VALUES (@First_Name, @Last_Name, @Address, @Email, @Phone_Number, @Password, @Type)";

                try
                {
                    con.Open();
                    SqlCommand cmdIns = new SqlCommand(sqlIns, con);
                    cmdIns.Parameters.AddWithValue("@First_Name", firstName);
                    cmdIns.Parameters.AddWithValue("@Last_Name", lastName);
                    cmdIns.Parameters.AddWithValue("@Address", address);
                    cmdIns.Parameters.AddWithValue("@Email", email);
                    cmdIns.Parameters.AddWithValue("@Phone_Number", phone);
                    cmdIns.Parameters.AddWithValue("@Password", password);
                    cmdIns.Parameters.AddWithValue("@Type", type);
                    cmdIns.ExecuteNonQuery();
                    cmdIns.Dispose();
                    cmdIns = null;
                    con.Close();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred while submitting the defect.");
                    throw new Exception(ex.ToString(), ex);
                }
            }
            else
                MessageBox.Show("Please complete the form!");
        }
    }
}
